﻿using System;
using System.Threading;
using System.Windows.Forms;
using GUI4UFramework.Management;
using GUI4UWindows.Keyboard;

namespace keyInputTester
{
    public partial class KeyInputTest : Form
    {
        #region Declare
        private string _eventMessage;
        private readonly DateTime _timeStarted;
        private readonly GameTime _gameTime;
        private readonly KeyboardReader keyboardReader;
        #endregion

        #region Initialize
        public KeyInputTest()
        {
            InitializeComponent();

            this.keyboardReader = new KeyboardReader();

            _timeStarted = DateTime.Now;

            _gameTime = new GameTime();
        }
        #endregion

        #region Message pump

        private void DoTimeCheck()
        {
            var time = DateTime.Now - _timeStarted;
            _gameTime.Update(time);
            this.keyboardReader.UpdateKeyboardInput(_gameTime);
            Thread.Sleep(10);
            CheckForKeyChanges();
            UpdateLabels();
        }



        #endregion

        private void CheckForKeyChanges()
        {
            var flags = this.keyboardReader.KeySwitches;
            if (flags.Changed == false) return;

            if (flags.ChangeText) _eventMessage = "Event change text";
            if (flags.SelectAll) _eventMessage = "Event select all";
            if (flags.EnterPressed) _eventMessage = "Event enter pressed";
            if (flags.DoCursorLeft) _eventMessage = "Event go cursor left";
            if (flags.DoCursorRight) _eventMessage = "Event go cursor right";
            if (flags.InsertText) _eventMessage = "Event insert text " + flags.TextToInsert;
            if (flags.Backspace) _eventMessage = "Event backspace";
            if (flags.KeyDelete) _eventMessage = "Event key delete";  

            flags.Reset();
        }

        private void UpdateLabels()
        {
            Text = _eventMessage;
            label1.Text = string.Format("Last pressed keys : {0}", this.keyboardReader.LastPressedKeys);
            label2.Text = string.Format("Fast repeat keys : {0}", this.keyboardReader.FastRepeatKeys);
            label3.Text = string.Format("ConfigText : {0}", this.keyboardReader.Text);
            label4.Text = string.Format("MousePressed keys : {0}", this.keyboardReader.PressedKeys);
        }
    }
}
